home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Crossword / Source / PlainState.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  127 lines

  1. /*
  2.  
  3. File States/Plain.m
  4.  
  5. The state object keeps track of information specific to the current state of the puzzle.  Different types of search need different types of information.  The basic state object is intended for plain depth first search.  It keeps track of squares, words, and a cache for letter counts.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10. #import <stdlib.h>
  11.  
  12. #import "Plain.h"
  13. #import "Puzzle.h"
  14. #import "FunctionCache.h"
  15. #import "Crossword.h"
  16. #import "CrosswordSquare.h"
  17.  
  18.  
  19. /* ————————————————————————————————————————————————————————————————————————————  */
  20.  
  21.  
  22. #define CACHESIZE        3000
  23.  
  24.  
  25. /* ————————————————————————————————————————————————————————————————————————————  */
  26.  
  27.  
  28. @implementation PlainState
  29.  
  30. - squareClass                            {    return [PlainSquare  class];    }
  31. - wordClass                                {    return [PlainWord  class];        }
  32. - getPuzzle                                {    return puzzle;                    }
  33. - getWords                                {    return words;                    }
  34. - getSquares                            {    return squares;                    }
  35. - (FunctionCache *) getCountCache        {    return countCache;                }
  36.  
  37.  
  38. /* ————————————————————————————————————————————————————————————————————————————  */
  39.  
  40.  
  41. - initPuzzle: (id) thePuzzle
  42. {
  43.     [super  init];
  44.     puzzle = thePuzzle;
  45.     countCache = [[FunctionCache  alloc]
  46.     
  47.             initKey: "*"
  48.             value: "!"
  49.             capacity: CACHESIZE
  50.             freeKey: free
  51.             freeValue: free ];
  52.     
  53.     [self  makeSquares];
  54.     [self  makeWords];
  55.     
  56.     return self;
  57. }
  58.  
  59.  
  60. - free
  61. {
  62.     [[squares  freeObjects]  free];
  63.     [[words  freeObjects]  free];
  64.     [countCache  free];
  65.     
  66.     return [super  free];
  67. }
  68.  
  69.  
  70. - makeSquares
  71. {
  72.     int        i, j;
  73.     int        rows, cols;
  74.     id        crossword, cell;
  75.     id        square;
  76.     id        squareClass;
  77.     
  78.     [crossword = [puzzle  getCrossword]  getNumRows: &rows  numCols: &cols];
  79.     squareClass = [self  squareClass];
  80.     
  81.     for (squares = [[List  alloc]  init], i = 0; i < rows; i++)
  82.     for (j = 0; j < cols; j++)
  83.     if ([cell = [crossword  cellAt: i: j]  isOpaque])
  84.     {
  85.         square = [[squareClass  alloc]  initPuzzle: puzzle  cell: cell];
  86.         [squares  addObject: square];
  87.         [cell  setData: square];
  88.     }
  89.     
  90.     return self;
  91. }
  92.  
  93.  
  94. - makeWords
  95. {
  96.     int        i, j;
  97.     id        wordSquares;
  98.     id        wordList, squareList;
  99.     id        word, square;
  100.     id        wordClass;
  101.     
  102.     wordList = [[puzzle  getCrossword]  getWords];
  103.     wordClass = [self  wordClass];
  104.     i = [wordList  count];
  105.     
  106.     while (i--) if ([[wordList  objectAt: i]  count] >= MINLETTERS)
  107.     {
  108.         word = [[wordClass  alloc]  initPuzzle: puzzle];
  109.         wordSquares = [[List  alloc]  init];
  110.         squareList = [wordList  objectAt: i];
  111.         j = [squareList  count];
  112.         
  113.         while (j--) 
  114.         {
  115.             square = [[squareList  objectAt: j]  getData];
  116.             [wordSquares  addObject: square];
  117.             [square  addToWord: word  at: j];
  118.         }
  119.         
  120.         [word  setSquares: wordSquares];
  121.     }
  122.     
  123.     return self;
  124. }
  125.  
  126.  
  127. @end